/*-
 * Copyright (c) 2006, 2007 FTS Team
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 4. Neither the name of the FTS Team nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 *      QueryParserTest.cpp
 *
 * $FTS$
 */
#include <stdio.h>
#include <string.h>

#include <QueryParser.hpp>
#include <ParseException.hpp>

#ifdef _NO_LEMMATIZER
#include <DefaultLemmatizerInterface.hpp>
#else
#include <TurglemLemmatizer.hpp>
#endif

using namespace FTSS;

void DescendTree(QueryToken * pToken, INT_32 & iLevel);

void DumpTree(QueryToken * pToken)
{
	INT_32 iLevel = 0;
	DescendTree(pToken, iLevel);
}

void DescendTree(QueryToken * pToken, INT_32 & iLevel)
{
	if (pToken == NULL) { fprintf(stdout, "pToken == NULL !!!\n"); return ; }

	++iLevel;
	if (pToken -> token_type == Q_WORD)
	{
		for (INT_32 iI = 0; iI < iLevel; iI++) { fprintf(stdout, "      "); }
		QueryWord * pTMP = dynamic_cast<QueryWord *>(pToken);
		fwrite(pTMP -> begin_token, pTMP -> end_token - pTMP -> begin_token, 1, stdout);
		fprintf(stdout, "(%d ", UINT_32(pTMP -> word_form));
		if      (pTMP -> word_type & C_FIRST_CAPITAL)                        { fprintf(stdout, "Fc "); }
		if      (pTMP -> word_type & C_ALL_CAPITAL)                          { fprintf(stdout, "AC "); }
		if      ((pTMP -> word_type & C_MIXED_LANGUAGE) == C_MIXED_LANGUAGE) { fprintf(stdout, "MIX "); }
		else if (pTMP -> word_type & C_CYRILLIC)                             { fprintf(stdout, "CYR "); }
		else if (pTMP -> word_type & C_LATIN)                                { fprintf(stdout, "LAT "); }
		if      (pTMP -> word_type & C_NUMBER)                               { fprintf(stdout, "NUM "); }
		if      (pTMP -> word_type & C_EXACT_MATCH)                          { fprintf(stdout, "! "); }
		fprintf(stdout, ")\n");
	}
	else if (pToken -> token_type != Q_END)
	{
		QueryOperator * pTMP = dynamic_cast<QueryOperator *>(pToken);

		for (INT_32 iI = 0; iI < iLevel; iI++) { fprintf(stdout, "      "); }
		switch(pTMP -> token_type)
		{
			case Q_OR:
				fprintf(stdout, "OR(%d)\n", 0/* pTMP -> child_count */);
				break;
			case Q_AND:
				fprintf(stdout, "AND(%d)\n", 0/* pTMP -> child_count */);
				break;
			case Q_NOT:
				fprintf(stdout, "NOT(%d)\n", 0/* pTMP -> child_count */);
				break;
			default:
				fprintf(stdout, "ERROR!");
		}
		QueryTokenList * pList = pTMP -> children;
		while (pList != NULL)
		{
			DescendTree(pList -> token, iLevel);
			pList = pList -> next;
		}
	}
	--iLevel;
}

int main(int argc, char ** argv)
{
	if (argc != 2) { fprintf(stdout, "usage: %s 'word1 word2 .... wordN'\n", argv[0]); return -1; }

	try
	{
#ifdef _NO_LEMMATIZER
		fprintf(stdout, "No lemmatizer.\n");
		DefaultLemmatizerInterface     oLemmatizer;
#else
		fprintf(stdout, "With lemmatizer.\n");
		TurglemLemmatizer     oLemmatizer("/usr/share/turglem");
#endif
		QueryParser oParser(oLemmatizer, (UCCHAR_8 *)argv[1], strlen(argv[1]));

		DumpTree(oParser.GetParsed());
	}
	catch(ParseException &e)  { fprintf(stderr, "ERROR at line %d, column %d\n", e.GetLine(), e.GetLinePos()); }
	catch(LogicException &e)  { fprintf(stderr, "ERROR %s\n", e.what()); }
	catch(...)                { fprintf(stderr, "ERROR: Ouch!\n"); }
}
// End.
